﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using FangDuShi_API.DBUtility;
using System.Data;
using System.Threading.Tasks;

namespace FangDuShi_API.DAL
{
    /// <summary>
    /// 手机验证码 数据类
    /// </summary>
    public class MobileCAPTCHA
    {
        public MobileCAPTCHA()
        { }
        /// <summary>
        /// 增加一条数据
        /// 有效时间 分钟
        /// </summary>
        public void Add(FangDuShi_API.Model.MobileCAPTCHA model, int validateMinitus)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_MobileCAPTCHA(");
            strSql.Append("FMobile,FCAPTCHACls,FCAPTCHA,FIsUse,FStartTime,FEndTime,FGenIP)");
            strSql.Append(" values (");
            strSql.Append("@FMobile,@FCAPTCHACls,@FCAPTCHA,@FIsUse,@FStartTime,DATEADD(mi,@validateMinitus,GETDATE()),@FGenIP)");
            SqlParameter[] parameters = {
					new SqlParameter("@FMobile", SqlDbType.NVarChar,40),
					new SqlParameter("@FCAPTCHACls", SqlDbType.NVarChar,6),
					new SqlParameter("@FCAPTCHA", SqlDbType.NVarChar,10),
					new SqlParameter("@FIsUse", SqlDbType.NVarChar,1),
					new SqlParameter("@FStartTime", SqlDbType.DateTime),
					new SqlParameter("@validateMinitus", SqlDbType.Int),
					new SqlParameter("@FGenIP", SqlDbType.NVarChar,15)};
            parameters[0].Value = model.FMobile;
            parameters[1].Value = model.FCAPTCHACls;
            parameters[2].Value = model.FCAPTCHA;
            parameters[3].Value = model.FIsUse;
            parameters[4].Value = model.FStartTime;
            parameters[5].Value = validateMinitus;
            parameters[6].Value = model.FGenIP;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
      
        /// <summary>
        /// 判断是否允许发送手机验证码
        /// 1、时间间隔为120秒
        /// 2、每个IP最多不超过20条(同一天)，特殊IP地址除外（系统需要建立IP白名单）
        /// 3、每个手机号码一天最多发送3条
        /// 4、经纪人忘记密码时，需要输入真是的手机号码和真实姓名，否则不发送验证码
        /// </summary>
        /// <param name="mobile">手机号码</param>
        /// <param name="interval">发送时间间隔</param>
        /// <param name="ipCount">每个IP同一天最多条数</param>
        /// <param name="mobileCount">每个手机号码同一天最多条数</param>
        /// <param name="ipAddress">ip地址</param>
        /// <param name="errorMsg">错误消息</param>
        /// <returns></returns>
        public bool CheckCanSendCAPTCHA(string mobile,int interval,int ipCount,int mobileCount,string ipAddress, out string errorMsg)
        {
            bool result = true;
            errorMsg = "";
            SqlParameter[] parameters=new SqlParameter[]{};
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 *  FROM  t_MobileCAPTCHA");
            strSql.Append(" where FMobile=@FMobile  AND DATEDIFF(ss,FStartTime,GETDATE())<=@interval");
             parameters = new SqlParameter[]{
					new SqlParameter("@FMobile", SqlDbType.NVarChar,40),
                    new SqlParameter("@interval", SqlDbType.Int)
			};
            parameters[0].Value = mobile;
            parameters[1].Value = interval.ToString();
            if (DbHelperSQL.Exists(strSql.ToString(), parameters))
            {
                errorMsg = "时间间隔不能小于" + interval.ToString()+ "秒";
                result = false;
            }

            if (result)
            {
                strSql = new StringBuilder();
                strSql.Append("select count(*) FROM  t_MobileCAPTCHA");
                strSql.Append(" where FGenIP=@FGenIP AND DATEDIFF(dd,FStartTime,GETDATE())=0");
                parameters = new SqlParameter[]{
					    new SqlParameter("@FGenIP", SqlDbType.NVarChar,15)
			    };
                parameters[0].Value = ipAddress;
                object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
                if (obj != null)
                {
                    if (int.Parse(obj.ToString()) >= ipCount)
                    {
                        errorMsg = "每个IP最多不超过"+ipCount.ToString()+"条(同一天)";
                        result = false;
                    }
                }
            }
            if (result)
            {
                strSql = new StringBuilder();
                strSql.Append("select count(*) FROM  t_MobileCAPTCHA");
                strSql.Append(" where FMobile=@FMobile AND DATEDIFF(dd,FStartTime,GETDATE())=0");
                parameters = new SqlParameter[]{
					    new SqlParameter("@FMobile", SqlDbType.NVarChar,40)
			    };
                parameters[0].Value = mobile;
                object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
                if (obj != null)
                {
                    if (int.Parse(obj.ToString()) >= mobileCount)
                    {
                        errorMsg = "每个手机号码同一天最多发送" + mobileCount.ToString()+"条";
                        result = false;
                    }
                }
            }

            return result;
        }
    }
}
